﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.backend
{
	public partial class User : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!IsPostBack)
            {
                if (Request.QueryString["status"] == null || Request.QueryString["page"] == null || Request.QueryString["search"]==null)
                {
                    bool status = false;
                    if (dropStatus.SelectedValue == "0") // mo tai khoan
                    {
                        status = true;
                    }
                    LoadData(txtSearch.Text, status);
                }
                else
                {
                    bool status = false;
                    if(Request.QueryString["status"]=="0")
                    {
                        status=true;
                    }
                    LoadData(Request.QueryString["search"], status);
                }

                
            }
		}
        public void LoadData(string search,bool status)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            if (Request.QueryString["search"] != null)
            {
                txtSearch.Text = Request.QueryString["search"];
            }            
            ltlPaging.Text = "";            
            int CurrentPage = 1;
            int Skip = 0;
            List<BLL.Admin.UserDao> List = BLL.Admin.UserHelper.List_User_Search(search, status);
            int TotalPage = (int)(List.Count / 10 + 1);

            if (Request.QueryString["page"] != null && int.Parse(Request.QueryString["page"]) > 0)
            {
                CurrentPage = int.Parse(Request.QueryString["page"]);
                Skip = (CurrentPage - 1) * 10;
            }

            rptUser.DataSource = List.Skip(Skip).Take(10);
            rptUser.DataBind();
            if (List.Count > 0)
            {
                for (int i = CurrentPage - 4; i < CurrentPage + 4; i++)
                {
                    if (i < 1)
                        continue;
                    if (i > TotalPage)
                        continue;
                    if (i == CurrentPage)
                        ltlPaging.Text += "<li><a class='current' href='" + path +"?status="+dropStatus.SelectedValue+"&search="+search+ "&page=" + i + "'>" + i + "</a></li>";
                    else
                        ltlPaging.Text += "<li><a href='" + path + "?status=" + dropStatus.SelectedValue + "&search=" + search + "&page=" + i + "'>" + i + "</a></li>";
                }                
            }
            else
            { }
        }
        public void LoadReset()
        {
            txtDienThoai.Text = "";
            txtEmail.Text = "";
            txtMatKhau.Text = "";
            txtUserName.Text = "";
            CheckMK.Checked = false;
            CheckMK.Visible = false;
            btSave.Visible = true;
            btUpdate.Visible = false;
            txtEmail.Enabled = true;
            bool status = false;
            if (dropStatus.SelectedValue == "0") // mo tai khoan
            {
                status = true;
            }
            LoadData(txtSearch.Text, status);
        }
        protected void btSave_Click(object sender, EventArgs e)
        {
            if (BLL.Admin.UserHelper.KiemTraEmailTonTai(txtEmail.Text) == true)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('ID/Email này đã tồn tại!');", true);
                return;
            }
            if (string.IsNullOrEmpty(txtMatKhau.Text))
            { 
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa Nhập mật khẩu!');", true);
                return;
            }
            BLL.Admin.UserHelper.Add_User(txtUserName.Text,txtMatKhau.Text,txtEmail.Text,txtDienThoai.Text,true,int.Parse(droRole.SelectedValue));
            bool status=false;
            if(dropStatus.SelectedValue=="0") // mo tai khoan
            {
                status = true;
            }
            LoadData(txtSearch.Text, status);
            LoadReset();
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            string mk = null;
            if (CheckMK.Checked == true)
            {
                if (string.IsNullOrEmpty(txtMatKhau.Text))
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa Nhập mật khẩu!');", true);
                    return;
                }
                mk = txtMatKhau.Text;
            }
            BLL.Admin.UserHelper.Update_User(int.Parse(lbUserId.Text),txtUserName.Text,mk,txtEmail.Text,txtDienThoai.Text,int.Parse(droRole.SelectedValue));
            bool status = false;
            if (dropStatus.SelectedValue == "0") // mo tai khoan
            {
                status = true;
            }
            
            LoadData(txtSearch.Text, status);
            LoadReset();

        }

        protected void btcancel_Click(object sender, EventArgs e)
        {
            LoadReset();
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            bool status = false;
            if (dropStatus.SelectedValue == "0") // mo tai khoan
            {
                status = true;
            }
            LoadData(txtSearch.Text, status);
        }

        protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int userid = int.Parse(e.CommandArgument.ToString());
                lbUserId.Text = userid.ToString();
                DAL.UserAdmin user = BLL.Admin.UserHelper.Get_UserId(userid);
                if (user != null)
                {
                    txtDienThoai.Text = user.DienThoai;
                    txtEmail.Text = user.Email;
                    txtUserName.Text = user.UserName;
                    droRole.SelectedValue = user.Role.ToString();
                    txtEmail.Enabled = false;
                    btSave.Visible = false;
                    btUpdate.Visible = true;
                    CheckMK.Checked = false;
                    CheckMK.Visible = true;
                    txtMatKhau.Enabled = false;
                }

            }
            if (e.CommandName == "Khoa")
            {
                int userid = int.Parse(e.CommandArgument.ToString());
                BLL.Admin.UserHelper.KhoaMoTaiKhoan(userid,false);
                bool status = false;
                if (dropStatus.SelectedValue == "0") // mo tai khoan
                {
                    status = true;
                }
                LoadData(txtSearch.Text, status);
                LoadReset();
            }
            if (e.CommandName == "Mo")
            {
                int userid = int.Parse(e.CommandArgument.ToString());
                BLL.Admin.UserHelper.KhoaMoTaiKhoan(userid, true);
                bool status = false;
                if (dropStatus.SelectedValue == "0") // mo tai khoan
                {
                    status = true;
                }
                LoadData(txtSearch.Text, status);
                LoadReset();
            }
        }

        protected void CheckMK_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckMK.Checked == true)
            {
                txtMatKhau.Enabled = true;
            }
            else
                txtMatKhau.Enabled = false;
        }
	}
}